package com.yny.administrator.android1607_meishi.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.yny.administrator.android1607_meishi.R;
import com.yny.administrator.android1607_meishi.adapter.MovieAdapter;
import com.yny.administrator.android1607_meishi.avtivity.ContentsActivity;
import com.yny.administrator.android1607_meishi.bean.Movie;
import com.yny.administrator.android1607_meishi.thread.LoadJsonTask;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ContentFragment extends Fragment implements LoadJsonTask.OnGetJsonListener,AdapterView.OnItemClickListener{


    private PullToRefreshGridView mGview;
    private MovieAdapter mAdapter;
    private List<Movie> mList=new ArrayList<>();
    private  int currentPage = 1;
    private String url;

    public ContentFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_content, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //获取控件
        initView(view);
        //设置上拉下拉样式
        initIndicator();
        mGview.setMode(PullToRefreshBase.Mode.BOTH);
        //获取对应json地址
        Bundle bundle = getArguments();
        url = bundle.getString("url");
        //开启异步任务
        loadNewData();
        mAdapter = new MovieAdapter(getActivity());
        mGview.setAdapter(mAdapter);
        mGview.setOnItemClickListener(this);
        //刷新数据
        mGview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            //下拉
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> pullToRefreshBase) {
                String label = DateUtils.formatDateTime(getActivity(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
                                | DateUtils.FORMAT_SHOW_DATE
                                | DateUtils.FORMAT_ABBREV_ALL);

                // Update the LastUpdatedLabel
                mGview.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                currentPage++;
                loadNewData();
            }
            //上拉
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> pullToRefreshBase) {
                currentPage++;
                loadNewData();
            }
        });
    }

    /**
     * 初始化数据
     */
    private void loadNewData() {
        new LoadJsonTask(this,mGview).execute(String.format(url, currentPage));
    }

    /**
     * @param view
     * 获取控件
     */
    private void initView(View view) {
        mGview = (PullToRefreshGridView) view.findViewById(R.id.gv);
    }

    /**
     * 上拉下拉样式设置
     */
    private void initIndicator() {
        //设置上部
        ILoadingLayout startLabels = mGview.getLoadingLayoutProxy(true, false);
        startLabels.setPullLabel("下拉刷新...");// 刚下拉时，显示的提示
        startLabels.setRefreshingLabel("正在刷新...");// 刷新时
        startLabels.setReleaseLabel("松开刷新...");// 下来达到一定距离时，显示的提示
        //设置下部
        ILoadingLayout endLabels = mGview.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉刷新...");// 刚下拉时，显示的提示
        endLabels.setRefreshingLabel("正在刷新...");// 刷新时
        endLabels.setReleaseLabel("松开刷新...");// 下来达到一定距离时，显示的提示
    }

    @Override
    public void getJsonListener(List<Movie> result) {
        mList.addAll(0,result);
        mAdapter.setData(mList);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Movie movie = mList.get(i);
        String type = movie.getType();
        if (type.equals("scheme")){
            Toast.makeText(getActivity(), "不存在视频", Toast.LENGTH_SHORT).show();
        }else {
            String url = movie.getUrl();
            String recommend_caption = movie.getRecommend_caption();
            Intent intent = new Intent(getActivity(), ContentsActivity.class);
            intent.putExtra("url",url);
            intent.putExtra("recommend_caption",recommend_caption);
            startActivity(intent);
        }
    }
}
